﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;
using System.Windows.Threading;
using FinPlusCommon;
using u = FinPlusUtility.Utility;
using log = Logger.Logger;

namespace FinPlusWPFControls
{
    public partial class FinPlusValueFunction : FinPlusControl
    {
        public override string Id { get; set; }
        public override string[] States { get { return new string[] { }; } }
        public override string[] TestStates { get { return new string[] { "ModelTest", "ViewTest", "IsValid" }; } }
        public override IFinPlusFunctions Model { get { return null; } }
        public override string ModelTest { get { return ModelToString(); } }
        public override bool IsValid { get; set; }

        public string ViewTest { get { return ToString(); } }

        public string Function { get { return GetValue(FunctiontProperty).ToString(); } set { SetValue(FunctiontProperty, value); } }
        public new bool IsEnabled { get { return (bool)GetValue(IsEnabledProperty); } set { } }

        public static readonly DependencyProperty FunctiontProperty = DependencyProperty.Register(Propertys.Function.ToString(), typeof(string), typeof(FinPlusValueFunction), new UIPropertyMetadata(Callback));
        public new static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register(Propertys.IsEnabled.ToString(), typeof(bool), typeof(FinPlusValueFunction));

        private bool _hasSetEnable;
        private enum Propertys { Function, IsEnabled }

        //construct
        public FinPlusValueFunction()
        {
            InitializeComponent();
            IsValid = true;
        }

        //common control interface
        public override void Dock(object control, string dock)
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        public override void Initialise()
        {

        }

        public override void CommandRouter(IFinPlusFunction func)
        {
            try
            {
                CaptureCommand(func);

                switch ((Commands)Enum.Parse(typeof(Commands), func.Name, true))
                {
                    case Commands.Open: Open(); break;
                    default: throw new Exception(string.Format("event not recognised {0}", func.Name));
                }
            }
            catch (Exception e)
            {
                IsValid = log.Log(Logger.Level.Warning, e.Message, e);
            }
        }

        public override void Dispose() { }

        //public
        public void SetControl()
        {
            //permission
            if (!_hasSetEnable)
            {
                 button.IsEnabled = IsEnabled;
                _hasSetEnable = true;
            }
        }

        public new string ToString()
        {
            //TODO
            return string.Empty;
        }

        public string ModelToString()
        {
            //TODO
            return string.Empty;
        }

        //private
        private void Open()
        {
            FinPlusWindow.Open(new Function((object[,]) u.StringToArray2d(Function)), "FuncEdit", Env);
        }

        //cmds
        private new enum Commands { Open }

        //events
        private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs a) { ((FinPlusValueFunction)d).SetControl(); }
        private void Function_Open(object sender, RoutedEventArgs a) { CommandRouter(new Function(Commands.Open.ToString())); }
    }
}